/*
 * Copyright(c) 2018-2018, YuanYue Group, Inc. All rights reserved.
 *
 * This software is the confidential and proprietary information of YuanYue Group, Inc.
 * You shall not disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into with YuanYue Group.
 */
package net.wsfcp.android.ui.view.my.activity;

import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;

import net.wsfcp.android.R;
import net.wsfcp.android.base.BaseActivity;
import net.wsfcp.android.http.mvp.contract.FindPasswordContract;
import net.wsfcp.android.event.LoginEvent;
import net.wsfcp.android.http.mvp.presenter.FindPasswordPresenter;
import net.wsfcp.android.utils.InputStreamUtils;
import net.wsfcp.android.utils.SpUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.io.IOException;
import java.io.InputStream;

import butterknife.BindView;

/**
 * LoginActivity.java是全网覆盖平台的。
 *
 * @author admin
 */
//找回密码
public class FindPasswordActivity extends BaseActivity<FindPasswordPresenter> implements FindPasswordContract.FindPasswordView {
    @BindView(R.id.rl_left)
    RelativeLayout rlLeft;
    @BindView(R.id.txt_title)
    TextView txtTitle;
    @BindView(R.id.btn_right)
    Button btnRight;

    @BindView(R.id.edt_username)
    EditText edtUsername;
    @BindView(R.id.edt_mail)
    EditText edtMail;
    @BindView(R.id.edt_code)
    EditText edtCode;
    @BindView(R.id.img_code)
    ImageView imgCode;
    @BindView(R.id.txt_find)
    TextView txtFind;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_find_password;
    }

    @Override
    protected FindPasswordPresenter loadPresenter() {
        return new FindPasswordPresenter();
    }

    @Override
    protected void initData() {
        setmTitle("找回密码");
        setLeftClick(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        mPresenter.getImageCode();
    }

    @Override
    protected void initListener() {
        txtFind.setOnClickListener(this);
        imgCode.setOnClickListener(this);
    }

    @Override
    protected void otherViewClick(View view) {
        switch (view.getId()) {
            case R.id.txt_find:
                showPg();
                mPresenter.findPassword(getUserName(), getMail(), getCode());
                break;
            case R.id.img_code:
                mPresenter.getImageCode();
                break;


        }

    }

    @Override
    protected void initView() {
        btnLeft = rlLeft;
        mTitle = txtTitle;
        btRight = btnRight;

        if (!TextUtils.isEmpty(SpUtils.getInstance().getUserName())) {
            edtUsername.setText(SpUtils.getInstance().getUserName());
            edtCode.requestFocus();
        }
    }


    @Override
    public String getUserName() {
        return edtUsername.getText().toString().trim();

    }

    @Override
    public String getMail() {
        return edtMail.getText().toString().trim();
    }


    @Override
    public String getCode() {
        return edtCode.getText().toString().trim();
    }

    @Override
    public void findPasswordSuccess() {
        dismissPg();
        toast("密码已发送到您的邮箱,请查收");
        EventBus.getDefault().post(new LoginEvent(2));
    }

    @Override
    public void findPasswordFail(String failMsg) {
        dismissPg();
        toast(failMsg);
    }


    @Override
    public void getImageCodeSuccess(InputStream inputStream) throws IOException {
        Glide.with(this).load(InputStreamUtils.input2byte(inputStream)).apply(new RequestOptions().placeholder(R.mipmap.ic_launcher).centerInside()).into(imgCode);
    }

    @Override
    public void getImageCodeFail(String failMsg) {

    }


    public boolean checkNull() {
        boolean isNull = false;
        if (edtUsername.getText().toString().trim().length() == 0) {
            toast("用户名不能为空");
            isNull = true;
            dismissPg();
        } else if (edtMail.getText().toString().trim().length() == 0) {
            toast("密码不能为空");
            isNull = true;
            dismissPg();
        } else if (edtCode.getText().toString().trim().length() == 0) {
            toast("验证码不能为空");
            isNull = true;
            dismissPg();
        }
        return isNull;
    }


    //接收订阅的事件
    @Subscribe(threadMode = ThreadMode.POSTING)
    public void login(LoginEvent event) {
        switch (event.getCode()) {
            case 0:   // 退出登录
                finish();
                break;
            case 1:   // 登录
                finish();
                break;
            case 2:   // 忘记密码
                finish();
                break;

        }

    }
}